<template>
  <div>
    <h1>新建文章</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="上级分类">
        <el-select v-model="form.categories" multiple>
          <el-option
            v-for="item in categories"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称">
        <el-input v-model="form.title"></el-input>
      </el-form-item>

      <el-form-item label="文中详情">
          <vue-editor v-model="form.body"    use-custom-image-handler
        @image-added="handleImageAdded"
 />
      </el-form-item>
      <el-button @click="save()">创建</el-button>
    </el-form>
  </div>
</template>

<script>
import { VueEditor } from "vue2-editor";
export default {
  data() {
    return {
      content:'',
      categories: [],
      form: {},
    };
  },
  created() {
    this.getList();
  },
 components: { VueEditor },
  methods: {
    //   副文本编辑器  用formdata上传
    async handleImageAdded(file, Editor, cursorLocation, resetUploader) {
      console.log('123')
      const formData = new FormData();
      formData.append("file", file);
      const res = await this.$http.post("upload", formData);
      Editor.insertEmbed(cursorLocation, "image", res.data.url);
      resetUploader();
    },
    //   提交上传
    async save() {
      const res = await this.$http.post("/rest/articles", this.form);
      console.log(res);
    },
    // 关联的是获取新建新建分类列表
    async getList() {
      const res = await this.$http.get(`rest/categories`);
      this.categories = res.data;
    },
  },
};
</script>